# -*- coding: utf-8 -*-
# @Author : cm_Simple
# @Time : 2022/1/4
# @Remark: 水浸 Server 层
import time

from mxsoftpy import Server
from db.common.IOTManager.Water.water import (WaterCheckDB,
                                              WaterDevicesDB,
                                              WaterDataDB)
from utils.public.params_method import get_pagination_params, paging_data


class WaterCheckServer(Server):
    """
    水浸设备实体 Server
    """
    __db__ = WaterCheckDB

    # def __init__(self):
    #     super().__init__()
    # self.like_name = 'Water'
    # self.need_check_key = 'name'

    def read(self, **water_data) -> dict:
        """
        查看水浸设备
        :param water_data: 查询条件
        :return: 查询数据
        """

        query = get_pagination_params(water_data)  # 2022-01-14 water 修改
        # query = {}

        # key_list = water_data.keys()
        # query['mxdevicetype__like'] = r'*\\%s\\*' % self.like_name
        # if self.need_check_key in key_list:
        #     query['mxlabel__nclike'] = '*'+water_data.pop(self.need_check_key)+'*'
        # return self.db.read(**water_data)
        res_db_dict: dict = self.db.read(**water_data)

        res_dict = paging_data(res_db_dict["data"], **query)
        res_db_dict["data"] = res_dict.pop("data", [])
        res_db_dict.update(res_dict)

        return res_db_dict

    def device_info(self, device_list, state_dict: dict = None):
        """
        设备信息
        @param state_dict:
        @param device_list:
        @return:
        """
        res_db_dict: dict = self.db.device_info(device_list, state_dict)
        return res_db_dict


class WaterDevicesServer(Server):
    """
    水浸设备实体 Server
    """
    __db__ = WaterDevicesDB

    def read(self) -> dict:
        """
        查看水浸设备
        :return: 水浸设备
        """
        return self.db.read()


class WaterDataServer(Server):
    """
    水浸设备实体 Server
    """
    __db__ = WaterDataDB

    def read(self, **water_data) -> dict:
        """
        查看文档
        :param water_data: 查询条件
        :return: 查询数据
        """
        water_data_keys = water_data.keys()
        water_data['table_key'] = dict()
        water_data['key_contrast'], water_data['contrast_last'] = dict(), dict()
        # 根据不同的参数，给代码传不同参数，控制代码不同走向
        if 'alert_time' in water_data_keys:
            water_data['table_key']['days'] = int(water_data.pop('alert_time'))
            water_data['table_key']['count'] = 0
        elif 'start_time' in water_data_keys and 'end_time' in water_data_keys:
            water_data['table_key']['start_time'] = int(
                time.mktime(time.strptime(water_data.pop('start_time'), '%Y-%m-%d %H:%M:%S')))
            water_data['table_key']['end_time'] = int(
                time.mktime(time.strptime(water_data.pop('end_time'), '%Y-%m-%d %H:%M:%S')))
        elif 'contrast_type' in water_data_keys:
            # 不同时间对比的时间间隔，0 为日对比，1 为周对比，2 为月对比
            contrast_type_dict = {0: 24 * 60 * 60, 1: 7 * 24 * 60 * 60, 2: 30 * 24 * 60 * 60}
            contrast = contrast_type_dict.get(int(water_data['contrast_type']), 0)
            end_time = int(time.time())
            start_time = end_time - contrast
            # 本期起止时间
            water_data['key_contrast']['start_time'] = start_time
            water_data['key_contrast']['end_time'] = end_time
            # 上期起止时间
            water_data['contrast_last']['start_time'] = start_time - contrast
            water_data['contrast_last']['end_time'] = start_time - 1
        else:
            water_data['table_key']['days'] = 100
            water_data['table_key']['count'] = 40
        return self.db.read(**water_data)
